//
//  StatusCell.swift
//  SinaWeibo
//
//  Created by 林柱强 on 16/2/3.
//  Copyright © 2016 林柱强. All rights reserved.
//

import UIKit
import SDWebImage

//let StatusCellMargin: CGFloat = 12
//let StatusCellIconWidth: CGFloat = 35

enum StatusTableViewCellIdentifier: String {
    case OriginalCell = "OriginalCell"
    case ForwardCell = "ForwardCell"
    
    // 如果在枚举中利用static修饰一个方法 , 相当于类中的class修饰方法
    // 如果调用枚举值的rawValue, 那么意味着拿到枚举对应的原始值
    static func cellID(status: Status) -> String {
        return status.retweeted_status != nil ? ForwardCell.rawValue : OriginalCell.rawValue
    }
}

class StatusTableViewCell: UITableViewCell {

    //保存配图的宽度约束
    var pictureWidthCons: NSLayoutConstraint?
    //保存配图的高度约束
    var pictureHeightCons: NSLayoutConstraint?
    //保存配图的顶部约束
    var pictureTopCons: NSLayoutConstraint?
    
    //数据模型
    var status: Status? {
        didSet {
            //设置顶部视图
            topView.status = status
            //设置正文
            contentLabel.text = status?.text
            //设置配图尺寸
            pictureView.status = status?.retweeted_status != nil ? status?.retweeted_status : status
            //1.1根据模型计算配图尺寸
            //注意：计算尺寸需要用到模型，所以先传递模型
            let size = pictureView.calculateImageSize()
//            print("配图的尺寸\(size)")
            //1.2设置配图尺寸
            pictureWidthCons?.constant = size.width
            pictureHeightCons?.constant = size.height
            pictureTopCons?.constant = size.height == 0 ? 0 : 10
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        //初始化UI
        setupUI()
    }
    
    func setupUI() {
        //添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(footerView)
        
        
        //设置约束
        topView.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: contentView, size: CGSize(width: screenWidth, height: 60))
        
        contentLabel.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: topView, size: nil, offset: CGPoint(x: 10, y: 10))
        
        footerView.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: screenWidth, height: 44), offset: CGPoint(x: -10, y: 10))
        
    }
    
    func rowHeight(status: Status) -> CGFloat {
        // 1.为了能够调用didSet,计算配图高度
        self.status = status
        // 2.强制更新界面
        self.layoutIfNeeded()
        // 3.返回底部视图最大的Y值
        return CGRectGetMaxY(footerView.frame)
    }
    
    //MARK: 懒加载控件
    /// 顶部视图
    private lazy var topView: StatusTableViewTopView = StatusTableViewTopView()
    //微博正文
    lazy var contentLabel: UILabel = UILabel(title: "", size: 14, color: UIColor.lightGrayColor(), margin: 10)
    /// 配图
    lazy var pictureView: StatusTableViewPictureView = StatusTableViewPictureView()
    /// 底部工具栏
    lazy var footerView: StatusTableViewBottomView = StatusTableViewBottomView()
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
